package com.softer.chengxin.district.service.impl;

import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.district.dao.DistrictDao;
import com.softer.chengxin.district.entity.District;
import com.softer.chengxin.district.entity.DistrictStreet;
import com.softer.chengxin.district.service.DistrictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/10/13
 */
@Service
public class DistrictServiceImpl implements DistrictService {
    @Autowired
    private DistrictDao districtDao;

    /**
     * 查询地区树
     *
     * @return
     */
    @Override
    public List<DistrictStreet> getDistrictStreet() {
        List<DistrictStreet> streetList = districtDao.getDistrictStreet(0);
        if (null != streetList && streetList.size() > 0) {
            for (DistrictStreet street : streetList) {
                List<DistrictStreet> children = districtDao.getDistrictStreet(street.getId());
                street.setChildren(children);
                if (null != children && children.size() > 0) {
                    for (DistrictStreet s : children) {
                        List<DistrictStreet> c = districtDao.getDistrictStreet(s.getId());
                        s.setChildren(c);
                    }
                }
            }
            return streetList;
        }
        return new ArrayList<>();
    }

    /**
     * 查询列表,填充下拉框
     *
     * @param parentId
     * @return
     */
    @Override
    public List<District> getDistrictByParentId(String parentId) {
        return districtDao.getDistrictByParentId(parentId);
    }


    /**
     * 新增
     *
     * @param district
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult add(District district) {
        //父级id
        String parentId = "0";

        int i = 0;
        if (null != district) {
            //判断是否新增区级
            if (StringUtils.isNotBlank(district.getDistrictName())) {
                district.setParentId(parentId);
                district.setName(district.getDistrictName());
                //判断当前区域是否已存在
                int id1 = districtDao.getCountId(district.getDistrictName(), parentId);
                if (id1 > 0) {
                    return new CommonResult(ResultEnum.FAIL.getCode(), "添加失败，地区已存在！");
                }
                i = districtDao.add(district);
                if (i > 0) {
                    //查询id
                    int id = districtDao.getId(district.getDistrictName(), parentId);
                    districtDao.updateAdminArea(id);
                    parentId = id + "";
                }
            } else {
                parentId = district.getDistrictId();
            }

            //判断是否新增街道级
            if (StringUtils.isNotBlank(district.getStreetName())) {
                district.setParentId(parentId);
                district.setName(district.getStreetName());
                //判断当前区域是否已存在
                int id1 = districtDao.getCountId(district.getStreetName(), parentId);
                if (id1 > 0) {
                    return new CommonResult(ResultEnum.FAIL.getCode(), "添加失败，街道已存在！");
                }
                i = districtDao.add(district);
                if (i > 0) {
                    int id = districtDao.getId(district.getStreetName(), parentId);
                    districtDao.updateAdminArea(id);
                    parentId = id + "";
                }
            } else {
                parentId = district.getStreetId();
            }
            //判断是否新增社区级
            if (StringUtils.isNotBlank(district.getCommunityName())) {
                district.setParentId(parentId);
                district.setName(district.getCommunityName());
                //判断当前区域是否已存在
                int id1 = districtDao.getCountId(district.getCommunityName(), parentId);
                if (id1 > 0) {
                    return new CommonResult(ResultEnum.FAIL.getCode(), "添加失败，社区已存在！");
                }
                i = districtDao.add(district);
                int id = districtDao.getId(district.getCommunityName(), parentId);
                districtDao.updateAdminArea(id);
            }
        }
        if (i == 0) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "添加失败，请稍后重试！");
        }
        return new CommonResult(ResultEnum.SUCCESS.getCode(), "添加成功！");
    }

    /**
     * 修改
     *
     * @param district
     * @return
     */
    @Override
    public CommonResult update(District district) {
        int i = districtDao.update(district);
        if (i == 0) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "修改失败，请稍后重试！");
        }
        return new CommonResult(ResultEnum.SUCCESS.getCode(), "修改成功！");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult delete(String id) {
        //查询id下是否有子区域
        int count = districtDao.getCountByParentId(id);
        if (count > 0) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "删除失败，包含子地区！");
        }
        int i = districtDao.delete(id);
        if (i == 0) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "删除失败，请稍后重试！");
        }
        return new CommonResult(ResultEnum.SUCCESS.getCode(), "删除成功！");
    }

    /**
     * 根具id 查詢區域信息，填充修改框
     *
     * @param id
     * @return
     */
    @Override
    public District getDistrictById(String id) {
        return districtDao.getDistrictById(id);
    }

}
